package 作业;
import d1026.DBHelper;
import java.sql.*;
import java.util.*;
@SuppressWarnings({"all"})
public class Jdbc1 {
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args){
        Jdbc1 j =new Jdbc1();
        while(true){
            System.out.println("**********************");
            System.out.println("*    1.查看课表       *");
            System.out.println("*    2.添加课程       *");
            System.out.println("*    3.修改课表       *");
            System.out.println("*    0.退出           *");
            System.out.println("**********************");
            System.out.println("请输入操作数:");
            int input = sc.nextInt();
            switch(input){
                case 1:

                    j.query();
                    break;
                case 2:
                    j.insert();
                    break;
                case 3:
                    j.update();
                    break;
                case 0:
                    System.exit(0);
                 default:
                     System.out.println("输入的操作数不正确...");
                     return;
            }

        }
    }
    public void query(){
        String sql = "select*from course where cweek = ?";
        System.out.println("查看第几周课表:");
        int cweek = sc.nextInt();
        try{
            List<Map<String,Object>> list = DBHelper.selectListMap(sql,cweek);
             Iterator<Map<String, Object>> iterator = list.iterator();
             while(iterator.hasNext()){
                 Map<String, Object> next = iterator.next();
                 System.out.println(next);
             }
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
    public void insert(){
        List<Object> list = new ArrayList<>( );
        String sql = "insert into course values(?,?,?,?)";
        System.out.println("输入课程名称:");
        String cname = sc.nextLine();
        list.add(cname);
        System.out.println("请输入课程名称，  节次  天次  周次");
        int ctime = sc.nextInt();
        list.add(ctime);
        int cday = sc.nextInt();
        list.add(cday);
        int cweek = sc.nextInt();
        list.add(cweek);
        try{
            DBHelper.update(sql,list.toArray());
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    public void update(){
      List<Object> list = new ArrayList<Object>();
      String sql = "update courses set cweek=?,cday=?,ctime=? where cweek=? and cday=? and cname=? ";
        System.out.println("请输入要修改的课程(周次 星期几 名称):");
        int cweek = sc.nextInt();
        int cday = sc.nextInt();
        String cname=sc.nextLine().intern();
        System.out.println("请输入要修改的课程(周次 星期几 节称):");
        int newWeek = sc.nextInt();
        int newDay = sc.nextInt();
        int newTime = sc.nextInt();
        list.add(newWeek);
        list.add(newDay);
        list.add(newTime);
        list.add(cweek);
        list.add(cday);
        list.add(cname);
        try{
            DBHelper.update(sql,list.toArray());
            System.out.println("修改成功...");
        }catch (SQLException e){
            e.printStackTrace();
        }


    }

    }



